﻿using System.Collections.Generic;
using System.Linq;
using MvcDesign.Core.CoreDataModel;
using MvcDesign.Core.CoreViewModel;

namespace MvcDesign.Core.CoreServices
{
    class PathViewService : IPathViewService
    {
        private readonly IConfiguration _configuration;

        public PathViewService(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public Path GetPathRoot()
        {
            return new Path { Name = _configuration.ForumName, Url = MvcHelper.UrlHelper.Content("~") };
        }

        public TopTab GetTopTabs()
        {
            var model = new TopTab
            {
                Applications = new List<TopTabApplication>(),
            };

            using (var domainModel = new DataAccess.DataContext())
            {
                model.CurrentApplicationName = (string)MvcHelper.RequestContext.RouteData.Values["application"];

                foreach (var application in domainModel.GetCollection<Application>().Where(a => a.Enabled))
                {
                    if (!application.Id.HasValue)
                    {
                        continue;
                    }

                    model.Applications.Add(new TopTabApplication { Id = application.Id.Value, Name = application.Name, Url = MvcHelper.UrlHelper.RouteUrl(new { application = application.Name }) });
                }
            }

            return model;
        }
    }
}
